#!/usr/bin/env bash

# This script sets environment variables and aliases to
# allow the user testing of a tiscamera build without the need
# of installation

#
# if you need this script bash free
# - change the shebang to sh
# - remove auto-completion at the bottom of the script
#

BASE="@TCAM_BINARY_DIR@"
SOURCE_DIR="@TCAM_SOURCE_DIR@"


if [ -z "$BASE" ]
then
    echo "Unable to find root dir of project."
    echo "Please set it manually."
    exit 1
fi

if [ -z "${TCAM_UVC_EXTENSION_DIR}" ]; then
    export TCAM_UVC_EXTENSION_DIR="${SOURCE_DIR}/data/uvc-extensions/"
else
    export TCAM_UVC_EXTENSION_DIR="${SOURCE_DIR}/data/uvc-extensions/:${TCAM_UVC_EXTENSION_DIR}"
fi

if [ -z "${PKG_CONFIG_PATH}" ]; then
    export PKG_CONFIG_PATH="${BASE}/data/:${BASE}/libs/tcam-property"
else
    export PKG_CONFIG_PATH="${BASE}/data/:${BASE}/libs/tcam-property:${PKG_CONFIG_PATH}"
fi

if [ -z "${C_INCLUDE_PATH}" ]; then
    export C_INCLUDE_PATH="${SOURCE_DIR}/libs/tcam-property/src:${SOURCE_DIR}/src/gstreamer-1.0"
else
    export C_INCLUDE_PATH="${SOURCE_DIR}/libs/tcam-property/src:${SOURCE_DIR}/src/gstreamer-1.0:${C_INCLUDE_PATH}"
fi

if [ -z "${CXX_INCLUDE_PATH}" ]; then
    export CXX_INCLUDE_PATH="${SOURCE_DIR}/libs/tcam-property/src:${SOURCE_DIR}/src/gstreamer-1.0"
else
    export CXX_INCLUDE_PATH="${SOURCE_DIR}/libs/tcam-property/src:${SOURCE_DIR}/src/gstreamer-1.0:${CXX_INCLUDE_PATH}"
fi

if [ -z "${GST_PLUGIN_PATH}" ]; then
    export GST_PLUGIN_PATH="${BASE}/lib"
else
    export GST_PLUGIN_PATH="${BASE}/lib/:${GST_PLUGIN_PATH}"
fi

if [ -z "${GI_TYPELIB_PATH}" ]; then
    export GI_TYPELIB_PATH="${BASE}/libs/tcam-property/src/"
else
    export GI_TYPELIB_PATH="${BASE}/libs/tcam-property/src/:${GI_TYPELIB_PATH}"
fi

# LD_RUN_PATH is used by ld during linking
# installed variables will not use this
if [ -z "${LD_RUN_PATH}" ]; then

    export LD_RUN_PATH="${BASE}/lib"
else
    export LD_RUN_PATH="${BASE}/lib:${LD_RUN_PATH}"
fi

# LD_LIBRARY_PATH is used at runtime by ldd to
# find libraries that are linked to your executable
if [ -z "${LD_LIBRARY_PATH}" ]; then
    export LD_LIBRARY_PATH="${BASE}/lib:"
else
    export LD_LIBRARY_PATH="${BASE}/lib:${LD_LIBRARY_PATH}"
fi

# LIBRARY_PATH is a list of directories containing static and
# shared libraries and is used before compilation
if [ -z "${LIBRARY_PATH}" ]; then
    export LIBRARY_PATH="${BASE}/lib/"
else
    export LIBRARY_PATH="${BASE}/lib:${LIBRARY_PATH}"
fi

# yes, this is really bad form
# many users use env.sh while still having
# a tiscamera version installed
# to ensure correct behavior this
# is sadly necessary
export PATH="${BASE}/bin/:${PATH}"

#
# auto completion
#
if [[ -d ${SOURCE_DIR}/data/bash-completion/ ]] && \
       ! find ${SOURCE_DIR}/data/bash-completion/. ! -name . -prune -exec false {} +
then
    for f in ${SOURCE_DIR}/data/bash-completion/*
    do
        # exclude CMakeLists.txt
        if [[ $f == *.txt ]]; then
            continue
        fi
        source "$f"
    done
fi
